package com.thinkgem.jeesite.modules.qx.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.TimeUtils;
import com.thinkgem.jeesite.modules.qx.entity.QxDataRule;
import com.thinkgem.jeesite.modules.qx.entity.QxLeagueMatches;
import com.thinkgem.jeesite.modules.qx.entity.QxMatch;
import com.thinkgem.jeesite.modules.qx.entity.QxMatchHistory;
import com.thinkgem.jeesite.modules.qx.entity.QxMontiorRule;
import com.thinkgem.jeesite.modules.qx.entity.QxTeam;
import com.thinkgem.jeesite.modules.qx.entity.QxTeamMatch;
import com.thinkgem.jeesite.modules.qx.service.QxDataRuleService;
import com.thinkgem.jeesite.modules.qx.service.QxMatchHistoryService;
import com.thinkgem.jeesite.modules.qx.service.QxMatchService;
import com.thinkgem.jeesite.modules.qx.service.QxTeamMatchService;
import com.thinkgem.jeesite.modules.qx.service.QxTeamService;
import com.thinkgem.jeesite.modules.qx.utils.QxCacheUtils;

@Service
@Lazy(false)
public class SixMathesTimesTask {
    private static final QxMontiorRule QxMontiorRule = null;

    private static int NUMBER = 1;
    
    @Autowired
    private QxTeamService qxTeamService;
    
    @Autowired
    private QxMatchService qxMatchService;
    
    @Autowired
    private QxTeamMatchService qxTeamMatchService;
    
    @Autowired
    private QxMatchHistoryService qxMatchHistoryService;
    
    @Autowired
    private QxDataRuleService qxDataRuleService;
    
//    @Scheduled(cron="0/15 * * * * ? ")
    @Scheduled(cron="0 0 1 1 * ? ")
    public void exec(){
        System.out.println("任务开始..........");
        System.out.println("这是第"+ NUMBER + "次任务");
        NUMBER++;
        System.out.println("任务结束..........");
      /*  List<QxTeam> teams = qxTeamService.findList(new QxTeam());
        QxMatch qxMatch = new QxMatch();
        for (QxTeam qxTeam : teams) { 
            qxMatch.setMainTeamId(qxTeam.getId());
            List<QxMatch> matchs = qxMatchService.findListByMatchHistory(qxMatch);
        }*/
        
    }    
    
    @Scheduled(cron="0 0 1 1 * ? ")
    public void exec2(){
        QxMatchHistory qxMatchHistory = new QxMatchHistory();
        qxMatchHistory.setDaytime( DateUtils.getDate("yyyyMMdd"));  
        List<QxMatchHistory> lists = qxMatchHistoryService.findListGroupByTeamid(qxMatchHistory);
        if (null != lists && lists.size() > 0) {
            List<String> ids = Lists.transform(lists, new Function<QxMatchHistory, String>() {
                @Override
                public String apply(QxMatchHistory input) {
                    return input.getTeamid();
                }
            });
            List<QxTeam> teams = qxTeamService.findByIds(ids);
            Map<String, QxTeam> teamMap = Maps.uniqueIndex(teams, new Function<QxTeam, String>() {
                @Override
                public String apply(QxTeam input) {
                    return input.getId();
                }
            });
            
            List<QxDataRule> rules = qxDataRuleService.findList(new QxDataRule());
            Map<String, QxDataRule> rulesMap = Maps.uniqueIndex(rules, new Function<QxDataRule, String>() {
                @Override
                public String apply(QxDataRule input) {
                    return input.getLeagueid();
                }
            });  
            
            StringBuffer sBuffer = new StringBuffer();
            int winCount = 0; 
            for (QxMatchHistory history : lists) {
                QxDataRule qxDataRule = new QxDataRule();
                QxTeam qxTeam = QxCacheUtils.geTeam(history.getTeamid());
                QxLeagueMatches league = QxCacheUtils.getQxLeagueMatches(history.getLeagueid());
                QxDataRule dataRule = rulesMap.get(history.getLeagueid());
                
                //胜利规则统计
                if (history.getResult() == 1) {
                    if (qxTeam.getNormalUnusual()*100 > dataRule.getUnusualTotal()*100/dataRule.getCountTotal()) {
                        join(sBuffer, QxCacheUtils.getMontiorRuleByName("异常状态").getId());
                    };
                    if (qxTeam.getWinCount()*100/qxTeam.getCount() > qxDataRule.getWinCount()*100/qxDataRule.getCountTotal()) {
                        join(sBuffer, QxCacheUtils.getMontiorRuleByName("胜率偏低").getId());
                    }
                    if (qxTeam.getWinScoreTotal()*100/qxTeam.getCount() < qxDataRule.getWinScoreTotal()*100/qxDataRule.getCountTotal()) {
                        join(sBuffer, QxCacheUtils.getMontiorRuleByName("均净胜球少").getId());
                    }
                    
                    winCount++;
                } else if (history.getResult() == 3) {
                    if (qxTeam.getLoseCount()*100/qxTeam.getCount() < qxDataRule.getLoseCount()*100/qxDataRule.getCountTotal()) {
                        join(sBuffer, QxCacheUtils.getMontiorRuleByName("负率太低").getId());
                    }
                    if (qxTeam.getLoseScoreTotal()*100/qxTeam.getCount() < qxDataRule.getLoseScoreTotal()*100/qxDataRule.getCountTotal()) {
                        join(sBuffer, QxCacheUtils.getMontiorRuleByName("均净失球少").getId());
                    }
                }
            }
            
        }
    }
    
    public static void main(String[] args) throws ParseException {
//        Date date = new SimpleDateFormat("yyyyMMdd").parse(new Date().toString());
        System.out.println(new Date().toString());
    }
    
    private StringBuffer join(StringBuffer sBuffer , String join) {
        if (sBuffer.length() != 0) {
            sBuffer.append("," + join);
        } else {
            sBuffer.append(join);
        }
        return sBuffer;
    }
}
